home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Book Chapters / 03 - Advanced Graphics / Example 7 / background.c next >
Text File  |  1995-02-28  |  3KB  |  125 lines

  1. //
  2. //    File: background.c
  3. //
  4. //    This file contains routines that draw the background.
  5. //
  6. //    2/19/95 -- Created by Mick
  7. //
  8.  
  9. // include files
  10.  
  11. #include "global.h"
  12.  
  13. #include "background.h"
  14.  
  15. #include "demo.h"
  16. #include "main.h"
  17.  
  18. // defines for this file
  19.  
  20. // global function declarations
  21.  
  22. void startupBackground( void );
  23. void shutdownBackground( void );
  24. void restoreBackground( Rect *inRect );
  25.  
  26. // global data owned by this file
  27.  
  28. // local function declarations
  29.  
  30. // static data
  31.  
  32. static GWorldPtr sBackgroundWorld;                                        // the GWorld of the background
  33. static PixMapHandle sBackgroundPixels;                            // the pixmap of the background
  34.  
  35. // functions
  36.  
  37.  
  38. //
  39. //    startupBackground -
  40. //
  41. //    Allocate and load any data needed for the background.
  42. //
  43.  
  44. void startupBackground( void )
  45. {
  46.     PicHandle backgroundPict;            // the background in pict form
  47.     CGrafPtr oldPort;                                    // the current port
  48.     GDHandle oldGDevice;                        // the current gdevice
  49.     
  50.     // load the pict
  51.     backgroundPict = GetPicture( kBackgroundPictResID );
  52.  
  53.     // create the gworld
  54.     NewGWorld( &sBackgroundWorld, 8, &( ( *backgroundPict )->picFrame ),
  55.             gAppColorTable, ( GDHandle )kNil, keepLocal );
  56.  
  57.     // get the pixmap
  58.     sBackgroundPixels = GetGWorldPixMap( sBackgroundWorld );
  59.     
  60.     // lock the pixels
  61.     LockPixels( sBackgroundPixels );
  62.  
  63.     // save the old port and device
  64.     GetGWorld( &oldPort, &oldGDevice );
  65.     
  66.     // set the new gworld
  67.     SetGWorld( sBackgroundWorld, ( GDHandle )kNil );
  68.     
  69.     // draw the picture
  70.     DrawPicture( backgroundPict, &( ( *backgroundPict )->picFrame ) );
  71.     
  72.     // dump the picture
  73.     ReleaseResource( ( Handle )backgroundPict );
  74.     
  75.     // restore the old port and device
  76.     SetGWorld( oldPort, oldGDevice );
  77.  
  78.     // lock the pixels
  79.     UnlockPixels( sBackgroundPixels );
  80.  
  81.     // dump the pict
  82.     ReleaseResource( ( Handle )backgroundPict );
  83. }
  84.  
  85.  
  86. //
  87. //    shutdownBackground -
  88. //
  89. //    Free any memory used by the background.
  90. //
  91.  
  92. void shutdownBackground( void )
  93. {
  94.     // dump the gworld
  95.     DisposeGWorld( sBackgroundWorld );
  96. }
  97.  
  98.  
  99. //
  100. //    restoreBackground -
  101. //
  102. //    Draw the background in the given rect.
  103. //
  104.  
  105. void restoreBackground( Rect *inRect )
  106. {
  107.     // lock the pixels
  108.     LockPixels( sBackgroundPixels );
  109.  
  110.     // make sure that the fore and back colors are correct to prevent colorize mode
  111.     ForeColor( blackColor );
  112.     BackColor( whiteColor );
  113.     
  114.     // Copy the screen's color table seed into the source pixmap.
  115.     // This will minimize CopyBits' setup time.
  116.     ( *( ( *sBackgroundPixels )->pmTable ) )->ctSeed = ( *( ( *gOffscreenPixels )->pmTable ) )->ctSeed;
  117.  
  118.     // copy the buffer to the screen
  119.     CopyBits( ( BitMap * )( *sBackgroundPixels ), ( BitMap * )( *gOffscreenPixels ), 
  120.             inRect, inRect, srcCopy, ( RgnHandle )kNil );
  121.  
  122.     // lock the pixels
  123.     UnlockPixels( sBackgroundPixels );
  124. }
  125.